package liuyao.webutils.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Configuration
public class BeanManager {

    @Bean("allAcceptExecutor")
    ThreadPoolExecutor allAcceptExecutor() {
        int tlen = Runtime.getRuntime().availableProcessors() * 2;
        return new ThreadPoolExecutor(tlen, tlen*2, 5, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(500),
                new ThreadFactory() {
                    AtomicInteger count = new AtomicInteger(0);
                    @Override
                    public Thread newThread(Runnable r) {
                        return new Thread(r, "ALL_ACCEPT_POLL_" + count.getAndIncrement());
                    }
                },
                (r, e) -> {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                    e.submit(r);
                }
        );
    }
}
